package studio.xiaoyun.core.parameter.criterion;

import java.util.Arrays;
import java.util.List;

import studio.xiaoyun.core.exception.CoreException;
import studio.xiaoyun.core.parameter.Parameter;

public class CriterionLike extends Criterion {
	private static final long serialVersionUID = -7098647105688686577L;
	private String propertyName;
	private Object value;

	protected CriterionLike(String propertynName, Object value) {
		this.propertyName = propertynName;
		this.value = value;
	}

	@Override
	public List<String> getPropertyName() {
		return Arrays.asList(propertyName);
	}

	@Override
	public String getQuery(Parameter parameter) {
		StringBuffer result = new StringBuffer();
		DataType dataType = this.getDataType(parameter, propertyName);
		switch (dataType) {
		  case STRING:
			String str = value.toString().replaceAll("%", "%%");
			result.append("%s like '").append(str).append("'");
			break;
		  default:
			throw new CoreException("属性" + propertyName + "不支持like");
		}
		return result.toString();
	}

}
